﻿using CDV.IRMS.WebAPI.TaskExecution.Utils;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace CDV.IRMS.WebAPI.TaskExecution
{
    /// <summary>
    /// 启动类
    /// </summary>
    public class Startup
    {
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="configuration">配置对象</param>
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        /// <summary>
        /// 配置对象
        /// </summary>
        public IConfiguration Configuration { get; }

        /// <summary>
        /// 配置服务
        /// </summary>
        /// <param name="services">服务集合</param>
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            //注入SwaggerAPI文档描述服务
            ServiceInjector.InjectSwaggerDoc(services);
            //注入任务管理交互服务
            ServiceInjector.InjectTaskManagerService(services, Configuration);
            //注入矩阵控制服务
            ServiceInjector.InjectMatrixService(services, Configuration);
            //注入资源管理服务
            ServiceInjector.InjectResourceManageService(services, Configuration);
            //注入缓存服务
            ServiceInjector.InjectCacheService(services);
            //注入Log4net
            ServiceInjector.InjectLog4net(services);
        }

        /// <summary>
        /// 配置应用程序
        /// </summary>
        /// <param name="app">IApplicationBuilder</param>
        /// <param name="env">IHostingEnvironment</param>
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            ServiceInjector.ServiceProvider = app.ApplicationServices;

            app.UseStaticFiles();

            app.UseSwagger();

            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "Task Execution API V1");
            });

            app.UseMvc();
        }
    }
}
